package earogya.pharmacy.request_for_drugs.ws;

import java.io.IOException;

import org.hibernate.HibernateException;

import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v251.message.ORM_O01;
import ca.uhn.hl7v2.model.v251.message.RDE_O11;
import ca.uhn.hl7v2.model.v251.segment.MSH;
import ca.uhn.hl7v2.parser.EncodingNotSupportedException;
import ca.uhn.hl7v2.parser.PipeParser;
import earogya.pharmacy.request_for_drugs.messages.ORM_O01_Handler;
import earogya.pharmacy.request_for_drugs.messages.RDE_O11_Handler;

public class HL7MessageHandler {

	public String parser(String message) {
		PipeParser p = new PipeParser();
		String ack = null;
		Message hapiMsg;

		try {
			// The parse method performs the actual parsing
			hapiMsg = p.parse(message);
			MSH mshSegment = (MSH) hapiMsg.get("MSH");

			String msgType = mshSegment.getMessageType().getMessageCode()
					.getValue();

			String msgTrigger = mshSegment.getMessageType().getTriggerEvent()
					.getValue();
			ack = identifyMessage(msgType, msgTrigger, hapiMsg);
			System.out.println(msgType + " " + msgTrigger);
		} catch (EncodingNotSupportedException e) {
			e.printStackTrace();
		} catch (HL7Exception e) {
			e.printStackTrace();
		} catch (HibernateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return ack;
	}

	private String identifyMessage(String messageCode, String triggerEvent,
			Message hapiMsg) throws HibernateException, IOException,
			HL7Exception {
		String ack = null;
		if (messageCode.equals("RDE")) {
			ack = this.identifyTriggerEventForRDE(triggerEvent, hapiMsg);
		} else if (messageCode.equals("ORM")) {
			ack = this.identifyTriggerEventForORM(triggerEvent, hapiMsg);
		} else {
			System.out.println("Unepected tigger event");
		}
		return ack;
	}

	private String identifyTriggerEventForORM(String triggerEvent,
			Message hapiMsg) throws HibernateException, IOException,
			HL7Exception {
		String ack = null;
		if (triggerEvent.equals("O01")) {
			ORM_O01 adtMsg = (ORM_O01) hapiMsg;
			ORM_O01_Handler order = new ORM_O01_Handler(adtMsg);
			ack = order.manageMessage();
		} else {
		}
		return ack;
	}

	private String identifyTriggerEventForRDE(String triggerEvent,
			Message hapiMsg) throws HibernateException, IOException,
			HL7Exception {
		String ack = null;
		if (triggerEvent.equals("O11")) {
			RDE_O11 adtMsg = (RDE_O11) hapiMsg;
			RDE_O11_Handler order = new RDE_O11_Handler(adtMsg);
			ack = order.manageMessage();
		} else {
		}
		return ack;
	}

	public static void main(String[] args) throws HL7Exception, IOException {

		String msgORMO01 = "MSH|^~\\&|GUI|CSTH|PHA|CSTH|20110610101010||RDE^O11||P|2.5.1\r"
				+ "EVN|O01|20030822172800|||1\r"
				+ "ORC|NE|1001^12|9999999^RX|||E|40^Q6H^10||20110610101010|1234|1235||PHR\r"
				+ "RXO|1120^urgent|500|500|mg||||ETU|Y\r";

		HL7MessageHandler h = new HL7MessageHandler();
		String ack = h.parser(msgORMO01);
		System.out.println(ack);

	}
}
